//
//  GoodsController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/8.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "GoodsController.h"

@interface GoodsController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *navBgView;
@property (nonatomic, strong) UIButton *navLeftBtn;
@property (nonatomic, strong) UIImageView *sanjiaoIcon;
@property (nonatomic, strong) UIView *downView;//底部按钮view
@property (nonatomic, strong) UIScrollView *myScrollview;

@end

@implementation GoodsController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNav];
    [self createDownBtnView];
    USER_DEFAULTS_SAVE(@"1", @"isDealer");//1跨境商品  2自营商品
    [self createUIScrollView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSString *fromMain =  USER_DEFAULTS_GET(@"fromMain");// 1表示从首页切换过来
    if ([fromMain isEqualToString:@"1"])
    {
        // 1表示从首页切换过来 过来之后清空
        USER_DEFAULTS_SAVE(@"", @"fromMain");
        
        //然后切换到已改价列表（以下是切花操作）
        USER_DEFAULTS_SAVE(@"", @"itemState");//清空该键值，表示搜索平台商品
        USER_DEFAULTS_SAVE(@"1", @"isDealer");
        self.navLeftBtn.frame = CGRectMake(15, self.navBgView.bottom-24, 44, 24);
        [self.navLeftBtn setTitle:@"全部" forState:UIControlStateNormal];
        self.sanjiaoIcon.frame = CGRectMake(self.navLeftBtn.right+1, self.navLeftBtn.top+3, 18, 14);
        
        UIButton *dealerBtn = [self.downView viewWithTag:1];
        dealerBtn.selected = YES;
        dealerBtn.backgroundColor = BtnBgColor;
        
        UIButton *selfBtn = [self.downView viewWithTag:2];
        selfBtn.selected = NO;
        selfBtn.backgroundColor = [UIColor whiteColor];
    }
    
    
    NSString *isDealer =  USER_DEFAULTS_GET(@"isDealer");
    if ([isDealer isEqualToString:@"2"])
    {
        self.myScrollview.contentOffset = CGPointMake(SCREENWIDTH, 0);
    }
    else {
        self.myScrollview.contentOffset = CGPointMake(0, 0);
    }
    
}

#pragma mark - 设置导航
- (void)setNav
{
    //创建自定义导航
    self.navBgView = [[UIView alloc]init];
    [self.view addSubview:self.navBgView];
    self.navBgView.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *))
    {
        self.navBgView.frame = CGRectMake(0, 0, SCREENWIDTH, 80+TopStatusHeight_xs);
    }
    else {
        // Fallback on earlier versions
        self.navBgView.frame = CGRectMake(0, 0, SCREENWIDTH, 80+TopStatusHeight);
    }
    
    self.navLeftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.navBgView addSubview:self.navLeftBtn];
    self.navLeftBtn.frame = CGRectMake(15, self.navBgView.bottom-24, 44, 24);
    self.navLeftBtn.titleLabel.font = FontMediumWithSize(18);
    self.navLeftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.navLeftBtn setTitle:@"全部" forState:UIControlStateNormal];
    [self.navLeftBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    [self.navLeftBtn addTarget:self action:@selector(navLeftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sanjiaoIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next_bg"]];
    [self.navBgView addSubview:self.sanjiaoIcon];
    self.sanjiaoIcon.frame = CGRectMake(self.navLeftBtn.right+1, self.navLeftBtn.top+3, 18, 14);
    
    //搜索按钮
    UIImageView *searchImage = [[UIImageView alloc] init];
    [self.navBgView addSubview:searchImage];
    searchImage.frame = CGRectMake(SCREENWIDTH-15-18, self.navBgView.bottom-18-2, 18, 18);
    
    searchImage.image = [UIImage imageNamed:@"sear_shopping"];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.navBgView addSubview:searchBtn];
    searchBtn.frame = CGRectMake(SCREENWIDTH-60, self.navBgView.bottom-30, 60, 30);
    [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 导航选择类别
- (void)navLeftBtnClick:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"仓库商品"])
    {
        USER_DEFAULTS_SAVE(@"0", @"itemState");//储存0，表示搜索自营仓库商品
        StorageShoppingVC *storage = [[StorageShoppingVC alloc] init];
        storage.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:storage animated:YES];
//        self.myScrollview.contentOffset = CGPointMake(SCREENWIDTH, 0);
    }
    else {
//        self.myScrollview.contentOffset = CGPointMake(0, 0);
        AllGoodsController *allGoods = [[AllGoodsController alloc] init];
        allGoods.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:allGoods animated:YES];
    }
}

#pragma mark - 导航搜索按钮点击事件
- (void)searchBtnClick:(UIButton *)sender
{
    SearchController *seach = [[SearchController alloc] init];
    seach.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:seach animated:YES];
}

#pragma mark - 创建底部按钮
- (void)createDownBtnView
{
    self.downView = [[UIView alloc] init];
    [self.view addSubview:self.downView];
    if (@available(iOS 11.0, *))
    {
        self.downView.frame = CGRectMake(0, SCREENHEIGHT-40-BottomHeight_xs-49, SCREENWIDTH, 40);
    } else {
        self.downView.frame = CGRectMake(0, SCREENHEIGHT-40-BottomHeight-49, SCREENWIDTH, 40);
    }
    self.downView.backgroundColor = [UIColor orangeColor];
    
    for (int i = 0 ; i < 2; i++)
    {
        CGFloat width = SCREENWIDTH/2;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.downView addSubview:btn];
        btn.frame = CGRectMake(width*(i%2), 0, width, 40);
        [btn setTitleColor:BlackTextColor forState:UIControlStateNormal];
        btn.titleLabel.font = FontWithSize(12);
        btn.tag = i+1;
        
        if (i == 0)
        {
            btn.selected = YES;
            btn.backgroundColor = BtnBgColor;
            [btn setTitle:@"我的跨境商品" forState:UIControlStateNormal];
            USER_DEFAULTS_SAVE(@"", @"itemState");//清空该键值，表示搜索平台商品
        }else {
            [btn setTitle:@"我的自营商品" forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            btn.selected = NO;
        }
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)btnClick:(UIButton *)sender
{
    for (UIButton *btn in sender.superview.subviews)
    {
        btn.selected = NO;
        btn.backgroundColor = [UIColor whiteColor];
    }
    sender.selected = YES;
    sender.backgroundColor = BtnBgColor;
    
    self.myScrollview.contentOffset = CGPointMake(SCREENWIDTH*(sender.tag-1), 0);
    if (sender.tag == 2)
    {//自营商品
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeToDealerNotific" object:nil];//发送通知改变到了自营商品 来获取自营商品中在上架还是在审核中
        
        USER_DEFAULTS_SAVE(@"2", @"isDealer");
        self.navLeftBtn.frame = CGRectMake(15, self.navBgView.bottom-24, 80, 24);
        [self.navLeftBtn setTitle:@"仓库商品" forState:UIControlStateNormal];
    }
    else {//跨境商品
        USER_DEFAULTS_SAVE(@"", @"itemState");//清空该键值，表示搜索平台商品
        USER_DEFAULTS_SAVE(@"1", @"isDealer");
        self.navLeftBtn.frame = CGRectMake(15, self.navBgView.bottom-24, 44, 24);
        [self.navLeftBtn setTitle:@"全部" forState:UIControlStateNormal];
    }
    self.sanjiaoIcon.frame = CGRectMake(self.navLeftBtn.right+1, self.navLeftBtn.top+3, 18, 14);
    
}

- (void)createUIScrollView
{
    self.myScrollview = [[UIScrollView alloc] init];
    self.myScrollview.frame = CGRectMake(0, self.navBgView.bottom, SCREENWIDTH, self.downView.top-self.navBgView.bottom);
    
    self.myScrollview.delegate = self;
    self.myScrollview.backgroundColor = [UIColor whiteColor];
    self.myScrollview.pagingEnabled = YES;
    self.myScrollview.scrollEnabled = YES;
    self.myScrollview.contentSize = CGSizeMake(0, 0);
    [self.view addSubview:self.myScrollview];
    self.myScrollview.backgroundColor = [UIColor lightGrayColor];
    
    for (int i = 0; i < 2; i++)
    {
        UIView *viewcon = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, self.myScrollview.height)];
        DealerSelectController *dealerSelectVC;
        BorderController *borderVC;
        if (i == 0)
        {
            borderVC = [[BorderController alloc] init];
            viewcon = borderVC.view;
            viewcon.frame = CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, self.myScrollview.height);
            [self.myScrollview addSubview:viewcon];
            //提价子视图控制器
            [self addChildViewController:borderVC];
        }else {
            dealerSelectVC = [[DealerSelectController alloc]init];
            viewcon = dealerSelectVC.view;
            viewcon.frame = CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, self.myScrollview.height);
            
            [self.myScrollview addSubview:viewcon];
            //提价子视图控制器
            [self addChildViewController:dealerSelectVC];
        }
    }
}

- (void)ShowHUDWithMessage:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = UIColorFromHex(0xFF9D9D9D);
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = NSLocalizedString(message, nil);
    hud.label.font = FontWithSize(12);
    [hud hideAnimated:YES afterDelay:HUDTime];
}

@end
